package edu.gu.hajo.gcl.parser;

import java.util.List;

import edu.gu.hajo.gcl.interp.IInterpreter;
import edu.gu.hajo.gcl.script.Script;

/**
 * Parser for a Tcl-like language. Main parser class delegating 
 * the work to specialized parsers.
 * 
 * @author hajo
 */

public final class Parser {

	private CommandParser commandParser;
	
	/*
	 * Act as builder
	 */
	public Parser( IInterpreter interp){
		BracesParser bracesParser = new BracesParser();
		VarParser varParser = new VarParser(bracesParser);
		CmdSubstParser cmdSubstParser = new CmdSubstParser();
		StringParser stringParser = new StringParser(interp, bracesParser, 
				varParser, cmdSubstParser);
		commandParser =  new CommandParser(interp, bracesParser, 
				varParser, cmdSubstParser, stringParser);
	}

	public List<String> parse(Script sc) {
		List<String> words = commandParser.parse(sc);
		return words;
	}

	// Utilites -------------------------------------------------

	public static void parseWordSeparator( Script script ){
		char ch;
		while( (ch = script.next()) != 0 ){
			if( ch == '\t' || ch == ' ' || ch == '\r'){
				continue;
			}else{
				script.putBack();
				break;
			}
		}
	}
	
	public static void parseCmdSeparator( Script script ){
		char ch;
		while( (ch = script.next()) != 0 ){
			if( ch == ';' || ch == '\n' ){
				continue;
			}else{
				script.putBack();
				break;
			}
		}
	}
	
	public static void parseSpace( Script script ){
		char ch;
		while( (ch = script.next()) != 0 ){
			if( ch == ';' || ch == '\n' || ch == '\t' || 
					ch == ' ' || ch == '\r'){
				continue;
			}else{
				script.putBack();
				break;
			}
		}
	}
}
